package com.ykdl.member.kid.adapters;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.Coupon_New_FragmentAdapter.ViewHolderOnLine;
import com.ykdl.member.kid.adapters.MyCouponAdapter.ViewHolder;
import com.ykdl.member.kid.beans.OnLineCouponBean;
import com.ykdl.member.kid.beans.TicketBean;
import com.ykdl.member.kid.gears.ShowFragmentActivity;
import com.ykdl.member.kid.marketcard.CouponinfoWebView;
import com.ykdl.member.kid.marketcard.MyCouponOnLineActivity;
import com.ykdl.member.kid.marketcard.MyCouponUnLineActivity;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.util.DisposalListUtils.ItemBean;
import com.ykdl.member.views.CircularImage;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
/********************************************************************************
 * 
 ** 作者： jiafeiyu 创始时间：2015-7-24 描述： 我的优惠券的adapter
 *********************************************************************************/
public class MyCouponOnLineAdapter extends BaseAdapter {

	private List<TicketBean>  mOffLineList;
	private BitmapUtils bitmapUtil;
	private Context mContext ;
	private LayoutInflater inflater ;
//	private List<TicketBean> mOffLineList;
	private List<OnLineCouponBean> mOnLineList;
	private List<ItemBean> mData;
	private int flag = -1;//用于区分是来之哪个 fragment   未使用 0  已使用 1 已过期 2
	/**
	 * 判断在线支付和到店支付是否有优惠券
	 */
	private boolean isOnLineNoCoupon = false;
	private boolean isOffLineNoCoupon = false;
	
	public MyCouponOnLineAdapter(Context context,int flag){
		mContext = context;
		bitmapUtil = new BitmapUtils(context);
		bitmapUtil.configDefaultLoadingImage(R.drawable.defult_head_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.defult_head_icon);
		inflater = LayoutInflater.from(context);
		this.flag = flag;
	}
	public void setmData(List<ItemBean> mData) {
		this.mData = mData;
	}
	public void setData(List<TicketBean>  tickets){
		this.mOffLineList = tickets;
	}
	
	public void setmOffLineList(List<TicketBean> mOffLineList) {
		this.mOffLineList = mOffLineList;
	}

	public void setmOnLineList(List<OnLineCouponBean> mOnLineList) {
		this.mOnLineList = mOnLineList;
	}

	@Override
	public int getCount() {
		if (mData!=null&&mData.size()>0) {
			return mData.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		convertView = initData(position, convertView);
		
		return convertView;
	}
	private int onLineLevel;
	private int offLineLevel;
	private View initData(int position, View convertView){
		/*if (mOnLineList!=null) {
			
			onLineLevel = mOnLineList.size()+1;
		}
		if (mOffLineList!=null) {
			
			offLineLevel = mOffLineList.size()+1;
		}
		if (isOnLineNoCoupon) {
			onLineLevel = 2;
		}
		if (isOffLineNoCoupon) {
			offLineLevel = 4;
		}
		if (position==0) {
			convertView = initTitle(1);//标题
		}else if(position==1&&isOnLineNoCoupon){
			convertView = initNoCoupon();//没有在线优惠券
		}else if(!isOnLineNoCoupon&&position>0&&position<onLineLevel){//有在线优惠券
			convertView = initOnLineCoupon(position, convertView);
		}else if(position==onLineLevel){//标题
			convertView = initTitle(2);
		}else if(position==(onLineLevel+1)&&isOffLineNoCoupon){//没有到店优惠券
			convertView = initNoCoupon();//没有在线优惠券
		}else if(position>(onLineLevel+1)&&!isOffLineNoCoupon){//有到店支付优惠券
			convertView = initMyCoupon(position, convertView);
		}*/
		
		ItemBean itemBean = mData.get(position);
		
		switch (itemBean.item_choose) {
		case 0:
			convertView = initTitle(1,itemBean.isHaveCoupon);//标题
			break;

		case 1:
			convertView = initTitle(2,itemBean.isHaveCoupon);//标题
			break;
		case 2:
			convertView = initOnLineCoupon(itemBean.position, convertView);
			break;
		case 3:
			convertView = initMyCoupon(itemBean.position, convertView);
			break;
		case 4:
			convertView = initNoCoupon(4);//
			break;
		case 5:
			convertView = initNoCoupon(5);//
			break;
		default:
			break;
		}
		
		
		Log.i("CouponFragment", "position="+position);
		return convertView;
		
	}
	
	private String store_id;
	public void setStore_id(String store_id) {
		this.store_id = store_id;
	}
	
	private View getView(int RescoureID){
		return  inflater.inflate(RescoureID, null);
	}
//------------------------------------------------到店支付可用优惠券--------------------------------------------------
	private View initOnLineCoupon(final int position, View convertView) {
		ViewHolderMyCoupon holder = null;
		 Object tag = null;
		 
		 OnLineCouponBean info = mOnLineList.get(position);
		 
		 
		if (convertView!=null) {
			
			tag = convertView.getTag();
		}
		if (null!=tag && tag instanceof ViewHolderOnLine) {
			holder = (ViewHolderMyCoupon) tag; 
		}else {
			holder = new ViewHolderMyCoupon();
			
			convertView = getView(R.layout.item_mycoupon_online);
			holder.tv_item_mycoupon_price = (TextView) convertView.findViewById(R.id.tv_item_mycoupon_price);
			holder.tv_item_coupon_online_type = (TextView) convertView.findViewById(R.id.tv_item_coupon_online_type);
			holder.tv_item_coupon_online_time = (TextView) convertView.findViewById(R.id.tv_item_coupon_online_time);
			holder.tv_rmb = (TextView) convertView.findViewById(R.id.tv_rmb);
			holder.rl_my_coupon_online = (RelativeLayout) convertView.findViewById(R.id.rl_my_coupon_online);
			convertView.setTag(holder);
		}
		holder.rl_my_coupon_online.setTag(info);
		holder.rl_my_coupon_online.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 页面详情的跳转
				
				OnLineCouponBean info = (OnLineCouponBean) v.getTag();
				Intent intent = new Intent(mContext, CouponinfoWebView.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KidAction.COUPON_ID, info.getTicket_id()+"");
				
				intent.putExtra(KidAction.URL_TYPE, 7);
				mContext.startActivity(intent);
			}
		});
		
		if (!TextUtils.isEmpty(info.getCoupon_num_1())) {
			holder.tv_item_mycoupon_price.setText(info.getCoupon_num_1());
		}
	
		if (!TextUtils.isEmpty(info.getScope_desc())) {
			holder.tv_item_coupon_online_type.setText(info.getScope_desc());
		}
		
		
		switch (flag) {
		case 1:
			//未使用
			String start = DateUtil.getTimeDate(info.getValid_begin_time());
			String end = DateUtil.getTimeDate(info.getValid_end_time());
			String content = "使用期限  "+start+"-"+end;
			
			holder.tv_item_coupon_online_time.setText(content);
			break;
		case 2:
			holder.tv_item_coupon_online_time.setText("已使用");		
			break;
		case 3:
			int color = mContext.getResources().getColor(R.color.white);
			holder.rl_my_coupon_online.setBackgroundResource(R.drawable.item_coupon_online_outofdate);
			holder.tv_item_mycoupon_price.setTextColor(color);
			holder.tv_item_coupon_online_type.setTextColor(color);
			holder.tv_item_coupon_online_time.setTextColor(color);
			holder.tv_rmb.setTextColor(color);
			holder.tv_item_coupon_online_time.setText("已过期");	
			break;

		default:
			break;
		}
		
		
		return convertView;
	}
	public class ViewHolderMyCoupon{
		RelativeLayout rl_my_coupon_online;
		TextView tv_item_mycoupon_price;
		TextView tv_item_coupon_online_type;//圣元示爱系列
		TextView tv_item_coupon_online_time;//使用期限  2015.7.1-2015.7.30
		TextView tv_rmb;
	}
	
//------------------------------------------------标题--------------------------------------------------
	private View initTitle(int number,boolean isHaveCoupon){
		View view = getView(R.layout.item_coupon_line_title);
		TextView mTitle = (TextView) view.findViewById(R.id.tv_coupon_title);
		RelativeLayout mBTView = (RelativeLayout) view.findViewById(R.id.rl_item_coupon_line);
		ImageView mArraw = (ImageView) view.findViewById(R.id.iv_arrows1);
		TextView mMore = (TextView)view.findViewById(R.id.tv_more_content);
		if (isHaveCoupon) {
			mArraw.setVisibility(View.VISIBLE);
			mMore.setText("更多");
			mMore.setTextColor(mContext.getResources().getColor(R.color.mycoupon_red));
		}else {
			mArraw.setVisibility(View.INVISIBLE);
			mMore.setText("暂无");
			mMore.setTextColor(mContext.getResources().getColor(R.color.mycoupon_gray));
		}
		
		switch (number) {
		case 1:
			mTitle.setText("在线支付");
			mBTView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					
					
					
//					Intent intent = new Intent(mContext,ShowFragmentActivity.class);
//					intent.putExtra("type", flag+2);
//					//intent.putExtra("store_id", 0);
//					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					mContext.startActivity(intent);
					
					Intent intent = new Intent(mContext,MyCouponOnLineActivity.class);
					intent.putExtra("choose", flag-1);
					
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(intent);
				}
			});
			break;
		case 2:
			mTitle.setText("到店支付");
			mBTView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
//					Intent intent = new Intent(mContext,ShowFragmentActivity.class);
//					intent.putExtra("type", flag+5);
//					intent.putExtra("store_id", store_id);
//					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					mContext.startActivity(intent);
					
					Intent intent = new Intent(mContext,MyCouponUnLineActivity.class);
					intent.putExtra("choose", flag-1);
					
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(intent);
				}
			});
			break;
		default:
			break;
		}
		
		return view;
	}	
	
//------------------------------------------------到店支付可用我的优惠券--------------------------------------------------
	public View initMyCoupon(int position, View convertView) {
		ViewHolder holder = null;
		Object tag  = null;
		
		if (convertView!=null) {
			tag = convertView.getTag();
		}
		if(null!=tag&&tag instanceof ViewHolder ){
			holder = (ViewHolder) tag;
		}else {
			holder = new ViewHolder();
			
			holder=new ViewHolder();
			convertView = inflater.inflate(R.layout.item_mycoupon_fragment_adapter, null);
			holder.ll_item_mycoupon_whole_back =(RelativeLayout) convertView.findViewById(R.id.ll_item_mycoupon_whole_back);
			holder.iv_mycoupon_fragment_back_logo =(CircularImage) convertView.findViewById(R.id.iv_mycoupon_fragment_back_logo);
			holder.iv_mycoupon_fragment_back_vip =(ImageView) convertView.findViewById(R.id.iv_mycoupon_fragment_back_vip);
			holder.tv_mycoupon_fragment_address =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment_address);
			holder.tv_mycoupon_fragment_ticket_number =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment_ticket_number);
			holder.tv_mycoupon_item_xilie =(TextView) convertView.findViewById(R.id.tv_mycoupon_item_xilie);
			holder.tv_mycoupon_out_of_date =(TextView) convertView.findViewById(R.id.tv_mycoupon_out_of_date);
			
			holder.ll_item_mycoupon_diyong =(LinearLayout) convertView.findViewById(R.id.ll_item_mycoupon_diyong);
			holder.tv_mycoupon_fragment_diyong_number =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment_diyong_number);
			
			holder.ll_item_mycoupon_manzeng =(LinearLayout) convertView.findViewById(R.id.ll_item_mycoupon_manzeng);
			holder.tv_mycoupon_fragment_manzeng_content =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment_manzeng_content);
			
			holder.ll_item_mycoupon_zhijiang =(LinearLayout) convertView.findViewById(R.id.ll_item_mycoupon_zhijiang);
			holder.tv_mycoupon_fragment_zhijiang_number =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment_zhijiang_number);
			
			holder.ll_item_mycoupon_manjian =(LinearLayout) convertView.findViewById(R.id.ll_item_mycoupon_manjian);
			holder.tv_mycoupon_fragment_manjian_content =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment_manjian_content);
			holder.tv_mycoupon_fragment__number =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment__number);
			
			convertView.setTag(holder);
		}
		
		initViews(holder,position);
		return convertView;
	}
	//优惠劵类型:1  # 抵用  2  # 满减  3  # 买赠  4  # 直降
	private void initViews(ViewHolder holder, int position) {
		if (mOffLineList==null||mOffLineList.size()==0) {
			return ;
		}
		TicketBean info = mOffLineList.get(position);
		switch (info.getCoupon_type()) {
		case 1:
			holder.ll_item_mycoupon_whole_back.setBackgroundResource(R.drawable.coupon_green_back);
			holder.ll_item_mycoupon_diyong.setVisibility(View.VISIBLE);
			holder.ll_item_mycoupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_mycoupon_zhijiang.setVisibility(View.GONE);
			holder.ll_item_mycoupon_manjian.setVisibility(View.GONE);
			holder.tv_mycoupon_fragment_diyong_number.setText(info.getCoupon_num_1());
			break;
		case 2:
			holder.ll_item_mycoupon_whole_back.setBackgroundResource(R.drawable.coupon_red_back);	
			holder.ll_item_mycoupon_diyong.setVisibility(View.GONE);
			holder.ll_item_mycoupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_mycoupon_zhijiang.setVisibility(View.GONE);
			holder.ll_item_mycoupon_manjian.setVisibility(View.VISIBLE);
			holder.tv_mycoupon_fragment_manjian_content.setText("满"+info.getCoupon_num_1()+"减");
			holder.tv_mycoupon_fragment__number.setText(info.getCoupon_num_2());
			break;
		case 3:
			holder.ll_item_mycoupon_whole_back.setBackgroundResource(R.drawable.coupon_yellow_back);
			holder.ll_item_mycoupon_diyong.setVisibility(View.GONE);
			holder.ll_item_mycoupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_mycoupon_zhijiang.setVisibility(View.GONE);
			holder.ll_item_mycoupon_manjian.setVisibility(View.VISIBLE);
			holder.tv_mycoupon_fragment_manjian_content.setText("买"+info.getCoupon_num_1()+"赠");
			holder.tv_mycoupon_fragment__number.setText(info.getCoupon_num_2());
//			holder.tv_mycoupon_fragment_manzeng_content.setText("买"+info.getCoupon_num_1()+"赠"+info.getCoupon_num_2());
			
			break;
		case 4:
			holder.ll_item_mycoupon_whole_back.setBackgroundResource(R.drawable.coupon_blue_back);
			holder.ll_item_mycoupon_diyong.setVisibility(View.GONE);
			holder.ll_item_mycoupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_mycoupon_zhijiang.setVisibility(View.VISIBLE);
			holder.ll_item_mycoupon_manjian.setVisibility(View.GONE);
			holder.tv_mycoupon_fragment_zhijiang_number.setText(info.getCoupon_num_1());
			break;

		default:
			break;
		}
		
		 if (info.isMembers_only()) {
			 
			 holder.iv_mycoupon_fragment_back_vip.setVisibility(View.VISIBLE); 
			 if (flag==2) {
				 holder.iv_mycoupon_fragment_back_vip.setBackgroundResource(R.drawable.mycoupon_vip_gray);
			}
		 }else {
			 holder.iv_mycoupon_fragment_back_vip.setVisibility(View.GONE);
		 }
		//劵号隐藏
//		if (info.isIs_ticket_code()) {
//			holder.tv_mycoupon_fragment_ticket_number.setVisibility(View.VISIBLE);
//			holder.tv_mycoupon_fragment_ticket_number.setText("券号:"+info.getTicket_id());
//		}else {
//			holder.tv_mycoupon_fragment_ticket_number.setVisibility(View.INVISIBLE);
//		}
		
		String url = info.getLogo().getFile_url();
		if (!TextUtils.isEmpty(url)) {
			 bitmapUtil.display(holder.iv_mycoupon_fragment_back_logo, url);
		}
		
		holder.tv_mycoupon_fragment_address.setText(info.getStore().getName());
		//TODO 没有系列的数据字段，后期添加
		holder.tv_mycoupon_item_xilie.setText(info.getDiscount_to());
		//holder
		switch (flag) {
		case 1:
			if (getOutofDate(info.getValid_end_time())==0) {
				holder.tv_mycoupon_out_of_date.setText("明天到期");
			}else {
				holder.tv_mycoupon_out_of_date.setText("还剩"+getOutofDate(info.getValid_end_time())+"天过期");
			}
			
			break;
		case 2:
			holder.tv_mycoupon_out_of_date.setText("已使用");		
			break;
		case 3:
			holder.tv_mycoupon_out_of_date.setText("已过期");	
			holder.ll_item_mycoupon_whole_back.setBackgroundResource(R.drawable.mycoupon_back_gray);
			
			break;

		default:
			break;
		}
		holder.ll_item_mycoupon_whole_back.setTag(info);
		 holder.ll_item_mycoupon_whole_back.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO 进行查询优惠券详情的跳转
					TicketBean info = (TicketBean) v.getTag();
					Intent intent = new Intent(mContext, CouponinfoWebView.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.putExtra(KidAction.COUPON_ID, info.getTicket_id());
					intent.putExtra(KidAction.URL_TYPE, 0);
					mContext.startActivity(intent);
				}
			});		
	}
	class ViewHolder {
		RelativeLayout ll_item_mycoupon_whole_back;
		CircularImage iv_mycoupon_fragment_back_logo;
		ImageView iv_mycoupon_fragment_back_vip;
		TextView tv_mycoupon_fragment_address;
		TextView tv_mycoupon_fragment_ticket_number;
		TextView tv_mycoupon_item_xilie;
		TextView tv_mycoupon_out_of_date;
		//抵用
		LinearLayout ll_item_mycoupon_diyong;
		TextView tv_mycoupon_fragment_diyong_number;
		//买赠
		LinearLayout ll_item_mycoupon_manzeng;
		TextView tv_mycoupon_fragment_manzeng_content;
		//直降
		LinearLayout ll_item_mycoupon_zhijiang;
		TextView tv_mycoupon_fragment_zhijiang_number;
		//满减
		LinearLayout ll_item_mycoupon_manjian;
		TextView tv_mycoupon_fragment_manjian_content;
		TextView tv_mycoupon_fragment__number;
	}
	
	
	
	private int getOutofDate(String date){
		if (!TextUtils.isEmpty(date)) {
			
			Date mdate = new Date(Math.round(Double.valueOf(date).doubleValue()*1000));
			
			long old = mdate.getTime();
			long now = System.currentTimeMillis();
			
			long num = old - now;
			
			
			long nums = num/1000;
			long numss = nums/3600;
			long numa = numss/24;
			
			int day = (int) numa;
			return day;
		}
		return 0;
	}
	//------------------------------------------------到店支付可用我的优惠券--------------------------------------------------
	
	
	//----------------------------------------没有优惠券展示---------------------------------------------------	
	private View initNoCoupon(int number){
		View convertView = getView(R.layout.item_mycoupon_online_no_coupon);
		switch (number) {
		case 4:
			convertView.findViewById(R.id.tv_no_coupon).setBackgroundResource(R.drawable.no_coupon_bg);
		
			break;
		case 5:
			convertView.findViewById(R.id.tv_no_coupon).setBackgroundResource(R.drawable.no_store_coupon_bg);
		
			break;
		default:
			break;
		}
		
		return convertView;
	}
		
		
}
